
// pages/my/my.js
const app = getApp()
const env = app.globalData.env

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    // 控制单一切换
    choose: false,
    showLogin: false,
    imgUrl: "/images/my/userImg.png",
    buttonList: ['全部', '全部', '全部', '全部', '全部', '全部'],
    needFlag:0
  },
  gotoWel(){
    wx.redirectTo({
  url: '/pages/start/start',
})
  },
  goClassManage() {
    wx.navigateTo({
      url: '/pages/classManage/classManage',
    })
  },

  exportExcel(e) {


    let task = e.currentTarget.dataset.card
    console.log(e.currentTarget.dataset.card)
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/exportExcel' : 'https://www.wantstong.com/api/exportExcel',
      method: 'POST',
      data: task,
      success: res => {
        console.log(res.data)

        wx.showLoading({
          title: '导出中...',
        })
       let excelTime= setInterval(function(){
          wx.downloadFile({
            url: res.data.http,
            success: res => {
              const tempFilePath = res.tempFilePath
              wx.saveFile({
                tempFilePath,
                success: res => {
                  clearInterval(excelTime)
                  const saveFilePath = res.savedFilePath;
                  wx.openDocument({
                    filePath: saveFilePath,
                    success: res => {
                      console.log("打开文档成功")
                    }
                  })
                  wx.hideLoading({
                    success: (res) => {},
                  })
                }
              })
            }
          })
        },2000)
   
      }
    })


  },

  searchPersonalCourse() {
    var date = new Date()
    let month = date.getMonth() + 1;
    let day = date.getDate()
    let hour = date.getHours()
    let min = date.getMinutes()
    console.log(month, day, hour, min)

    let _this = this
   
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/searchPersonalCourse' : 'https://www.wantstong.com/api/searchPersonalCourse',
      method: 'POST',
      data: {
        data: _this.data.userId
      },
      success: (res) => {
     
        _this.setData({
          // cardList: res.data.data,
          courseList: res.data.course,
        })
        wx.getStorage({
          key: 'courseIndex',
          success:res1=>{
            console.log("res",res)
            let index=res1.data
            
            _this.searchPersonalTask(res.data.course[index].courseId)
          },
      
        })

       
       
      }
    })
  },

  gotoTaskGrade(e) {
    console.log("gotoTaskGrade", e)
    let item = e.currentTarget.dataset.card

    wx.setStorage({
      data: item,
      key: 'taskBtn',
    })
    wx.navigateTo({
      url: '/pages/taskGrade/taskGrade',
    })

  },

  // 去往检查界面
  gotostartCheck(e) {
    let item = e.currentTarget.dataset.card

    wx.setStorage({
      data: item,
      key: 'checkItem',
    })
    wx.navigateTo({
      url: '/pages/startCheck/startCheck',
   
    })

  },
  isActive(e) {
    let _this=this;
    let currentIndex = e.currentTarget.dataset.index
    let oldIndex = this.data.currentIndex
    wx.setStorage({
      data:currentIndex ,
      key: 'courseIndex',
    })
    if (currentIndex == oldIndex != 0) {
      console.log("相等")
    } else {
      _this.searchPersonalTask(_this.data.courseList[currentIndex].courseId)
    
      this.setData({
        currentIndex: currentIndex,
        choose: true
      })
    }
  },

  rgb() {
    var r = Math.floor(Math.random() * 256);
    var g = Math.floor(Math.random() * 256);
    var b = Math.floor(Math.random() * 256);
    var color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
    return color;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const app = getApp()
    const globalData = app.globalData
    globalData.isLogin == false ? this.setData({
      "isLogin": globalData.isLogin
    }) : ""
    console.log("isLogin", this.data.isLogin)

    let _this = this
    wx.getStorage({
      key: 'user',
      success(res) {
        console.log(res)
        if(res.data.nickName){
          _this.setData({
            avatarUrl: res.data.avatarUrl,
            userName: res.data.nickName,
            userId: res.data.userId
          })
          _this.searchPersonalCourse()
        }else{
          _this.setData({
            needFlag:1
          })
        }
        
      }
    })
  },

  searchPersonalTask(courseId){
    let _this=this
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/searchPersonalTask' : 'https://www.wantstong.com/api/searchPersonalTask',

      method:'POST',
      data:{
        courseId:courseId,
        data:_this.data.userId

      },
      success:res=>{
        for(let i of res.data.data){
          i.color=_this.rgb()
        }
        _this.setData({
          cardList:res.data.data
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const app = getApp()
    const globalData = app.globalData
    globalData.isLogin == false ? this.setData({
      "isLogin": globalData.isLogin
    }) : ""
    console.log("isLogin", globalData.isLogin)

    let _this = this
    wx.getStorage({
      key: 'user',
      success(res) {
        console.log(res)
        _this.setData({
          avatarUrl: res.data.avatarUrl,
          userName: res.data.nickName,
          userId: res.data.userId
        })
        _this.searchPersonalCourse()
      }
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})